﻿/*  软件名：KuWoMusic最终版
    制作人：徐建敏
    学号：201631062323
    专业年级班级：软件工程2016级3班 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace KuWoMusic最终版
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
        }
        int next = 0;
        Songname songname = new Songname();      //读取歌名类
        List<Label> lstLbl = new List<Label>();   //新建一列Label存放歌词
        LyricFiles lyricFiles = new LyricFiles();   //歌词文件类
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //初始化，准备工作
            wmp.URL = "Music/" + songname.Filename()[next] + ".mp3";   //存入歌曲
            wmp.Ctlcontrols.stop();                                   //停止播放
            tcbSound.Value = 50;                                     //歌曲音量默认值为50
            pnlControlBar.BackColor = Color.FromArgb(127, 200, 200, 200);    //设置Panel灰色半透明
            lyricFiles.LoadLyric("Lyric/" + songname.Filename()[next] + ".lrc");  //读取对应歌词文件
            for (int i = 0; i < 16; i++)             //将歌词存入Label
            {
                if (lyricFiles.lstLyric.Count > 0)
                {
                    lstLbl.Add(new Label());
                    lstLbl[i].Size = new System.Drawing.Size(500, 30);
                    lstLbl[i].Location = new Point(300, 50 + 30 * i);
                    lstLbl[i].ForeColor = Color.White;
                    lstLbl[i].BackColor = Color.Transparent;
                    lstLbl[i].Font = new Font("微软雅黑", 14);
                    lstLbl[i].Visible = false;
                    this.Controls.Add(lstLbl[i]);
                }
            }
        }

        bool isPlay = false; //是否播放
        private void picPlay_Click(object sender, EventArgs e)
        {
            isPlay = !isPlay;
            if (isPlay)    //点击播放改变按钮背景和窗体背景，开始播放
            {
                wmp.Ctlcontrols.play();
                picPlay.BackgroundImage = Properties.Resources.pause_circle_o;
                this.BackgroundImage = Image.FromFile("Backimage/" + songname.Filename()[next] + ".jpg");

            }
            else            //再次点击暂停改变按钮背景
            {
                wmp.Ctlcontrols.pause();
                picPlay.BackgroundImage = Properties.Resources.play_circle_o;
            }
            tim.Enabled = true;   //tim开始工作
            lblShowDuration.Visible = true;   //显示歌曲时间
            picProgress.Visible = true;//显示进度条
        }
        int currentLyricLine = 0;    //歌词行数
        int progress;//歌曲进度
        Rectangle rectpro;//歌曲进度图型
        Graphics ghpro;//画进度
        private void tim_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < 16; i++)//开始显示歌词
            {
                lstLbl[i].Visible = true;
            }
            lblShowDuration.Text = wmp.Ctlcontrols.currentPositionString + '/' + wmp.currentMedia.durationString;  //实时显示歌曲时间
            double max, pos, per;
            max = wmp.currentMedia.duration;
            pos = wmp.Ctlcontrols.currentPosition;
            if (wmp.Ctlcontrols.currentPosition != 0)
            {
                per = pos / max;
                progress = (int)(per * picProgress.Width);
                rectpro = new Rectangle(0, 0, progress, picProgress.Height);
                ghpro = picProgress.CreateGraphics();
                ghpro.FillRectangle(new SolidBrush(Color.Red), rectpro);
            }   //歌曲进度条随着歌曲播放自动改变位置
            for (int i = 0; i < lyricFiles.lstLyric.Count; i++)//改变歌词显示
            {
                if (currentLyricLine < lyricFiles.lstLyric.Count)
                {
                    if (wmp.Ctlcontrols.currentPosition > lyricFiles.lstLyric[currentLyricLine].totSecond)
                    {
                        if (currentLyricLine < 8)//前一半歌词Label变色由上方逐渐变为中央
                        {
                            for (int j = 0; j < lstLbl.Count; j++)
                            {
                                lstLbl[j].Text = lyricFiles.lstLyric[j].strLyric;
                                if (j == currentLyricLine)
                                {
                                    lstLbl[j].ForeColor = Color.Cyan;
                                    lstLbl[j].Font = new Font("微软雅黑", 18);
                                }
                                else
                                {
                                    lstLbl[j].ForeColor = Color.White;
                                    lstLbl[j].Font = new Font("微软雅黑", 14);
                                }
                            }
                        }
                        else if (currentLyricLine > lyricFiles.lstLyric.Count - 9)//歌词加载完毕后面的Label为空
                        {
                            for (int j = 0; j < 15; j++)
                            {
                                lstLbl[j].Text = lstLbl[j + 1].Text;
                            }
                            lstLbl[15].Text = null;
                        }
                        else    //歌词Label中的文字依次推前
                        {
                            for (int j = 0; j < 15; j++)
                            {
                                lstLbl[j].Text = lstLbl[j + 1].Text;
                            }
                            lstLbl[15].Text = lyricFiles.lstLyric[currentLyricLine + 8].strLyric;
                        }
                        currentLyricLine++; //歌词行数+1
                    }
                }
            }
            if ((int)wmp.playState == 1)//当前歌曲播放完毕，播放下一首歌曲
            {
                if (allplay)//全部循环
                {
                    if (next == songname.Filename().Length - 1)//如果本售歌为最后一首，重新播放第一首
                    {
                        next = -1;
                    }
                    next++;
                }
                if (random)//随机播放
                {
                    Random rd = new Random();//获取随机数种子
                    int randnum;
                    do
                    {
                        randnum = rd.Next(0, songname.Filename().Length);
                    } while (randnum == next);//如果是当前播放歌曲，重新获取
                    next = randnum;
                }
                for (int x = 0; x < lstLbl.Count; x++)//移除本首歌全部歌词Label
                {
                    this.Controls.Remove(lstLbl[x]);
                }
                lstLbl.Clear();           //清空存放的歌词Label
                lyricFiles.lstLyric.Clear(); //清空所有歌词
                wmp.URL = "Music/" + songname.Filename()[next] + ".mp3"; //读取下一首歌，直接播放
                lyricFiles.LoadLyric("Lyric/" + songname.Filename()[next] + ".lrc"); //读取下一首歌歌词
                for (int i = 0; i < 16; i++)             //将歌词存入Label
                {
                    if (lyricFiles.lstLyric.Count > 0)
                    {
                        lstLbl.Add(new Label());
                        lstLbl[i].Size = new System.Drawing.Size(500, 30);
                        lstLbl[i].Location = new Point(300, 50 + 30 * i);
                        lstLbl[i].ForeColor = Color.White;
                        lstLbl[i].BackColor = Color.Transparent;
                        lstLbl[i].Font = new Font("微软雅黑", 14);
                        lstLbl[i].Visible = false;
                        this.Controls.Add(lstLbl[i]);
                    }
                }
                this.BackgroundImage = Image.FromFile("Backimage/" + songname.Filename()[next] + ".jpg");//更换相应背景图片
                currentLyricLine = 0;//歌词行数归0
            }
        }

        bool changeSound = false;//改变歌曲音量
        private void tcbSound_Scroll(object sender, EventArgs e)
        {
            changeSound = true;
        }

        private void tcbSound_MouseUp(object sender, MouseEventArgs e)
        {
            //鼠标弹起时，改变音量
            if (changeSound)
            {
                int num = tcbSound.Value;
                wmp.settings.volume = num;
            }
            changeSound = false;  //停止改变
            if (tcbSound.Value == 0)//如果音量为0，则静音
            {
                picSound.BackgroundImage = Properties.Resources.soundclose;
                soundclose = true;
            }
            else            //音量不为0，则取消静音
            {
                picSound.BackgroundImage = Properties.Resources.sound;
                soundclose = false;
            }
        }

        private void picRight_Click(object sender, EventArgs e)
        {
            //播放下一曲
            if (next == songname.Filename().Length - 1)
            {
                next = -1;
            }
            next++;
            for (int x = 0; x < lstLbl.Count; x++)//移除歌词Label
            {
                this.Controls.Remove(lstLbl[x]);
            }
            lstLbl.Clear();//清空歌词Label
            lyricFiles.lstLyric.Clear();//清空所有歌词
            if ((int)wmp.playState == 10 || (int)wmp.playState == 2) //点击时若没有歌曲播放或处于暂停，则下一曲不自动播放
            {
                wmp.URL = "Music/" + songname.Filename()[next] + ".mp3";
                wmp.Ctlcontrols.stop();
            }
            else  //点击时若有歌曲正在播放，则下一曲自动播放
            {
                wmp.URL = "Music/" + songname.Filename()[next] + ".mp3";
            }
            lyricFiles.LoadLyric("Lyric/" + songname.Filename()[next] + ".lrc");//读取相应歌词文件

            for (int i = 0; i < 16; i++)             //将歌词存入Label
            {
                if (lyricFiles.lstLyric.Count > 0)
                {
                    lstLbl.Add(new Label());
                    lstLbl[i].Size = new System.Drawing.Size(500, 30);
                    lstLbl[i].Location = new Point(300, 50 + 30 * i);
                    lstLbl[i].ForeColor = Color.White;
                    lstLbl[i].BackColor = Color.Transparent;
                    lstLbl[i].Font = new Font("微软雅黑", 14);
                    lstLbl[i].Visible = false;
                    this.Controls.Add(lstLbl[i]);
                }
            }
            this.BackgroundImage = Image.FromFile("Backimage/" + songname.Filename()[next] + ".jpg");//改变相应背景图片
            currentLyricLine = 0;//行数归0
        }

        private void picLeft_Click(object sender, EventArgs e)
        {
            //播放上一曲
            if (next == 0)//如果为第一曲，则播放最后一曲
            {
                next = songname.Filename().Length;
            }
            next--;
            for (int x = 0; x < lstLbl.Count; x++)
            {
                this.Controls.Remove(lstLbl[x]);
            }//移除歌词Label
            lstLbl.Clear();//清空歌词label
            lyricFiles.lstLyric.Clear();//清空所有歌词
            if ((int)wmp.playState == 10 || (int)wmp.playState == 2) //点击时若没有歌曲播放或处于暂停，则上一曲不自动播放
            {
                wmp.URL = "Music/" + songname.Filename()[next] + ".mp3";
                wmp.Ctlcontrols.stop();
            }
            else  //点击时若有歌曲正在播放，则上一曲自动播放
            {
                wmp.URL = "Music/" + songname.Filename()[next] + ".mp3";
            }
            lyricFiles.LoadLyric("Lyric/" + songname.Filename()[next] + ".lrc"); //读取相应歌词文件

            for (int i = 0; i < 16; i++)             //将歌词存入Label
            {
                if (lyricFiles.lstLyric.Count > 0)
                {
                    lstLbl.Add(new Label());
                    lstLbl[i].Size = new System.Drawing.Size(500, 30);
                    lstLbl[i].Location = new Point(300, 50 + 30 * i);
                    lstLbl[i].ForeColor = Color.White;
                    lstLbl[i].BackColor = Color.Transparent;
                    lstLbl[i].Font = new Font("微软雅黑", 14);
                    lstLbl[i].Visible = false;
                    this.Controls.Add(lstLbl[i]);
                }
            }
            this.BackgroundImage = Image.FromFile("Backimage/" + songname.Filename()[next] + ".jpg");//更换相应背景图片
            currentLyricLine = 0;//行数归0
        }
        bool soundclose = false;//是否静音
        private void picSound_Click(object sender, EventArgs e)
        {
            //点击喇叭，歌曲静音或取消静音
            soundclose = !soundclose;
            if (soundclose)
            {
                wmp.settings.mute = true;  //静音
                picSound.BackgroundImage = Properties.Resources.soundclose;  //静音时改变图标
                tcbSound.Value = 0;   //音量条变为0
            }
            else
            {
                wmp.settings.mute = false; //取消静音
                picSound.BackgroundImage = Properties.Resources.sound;//取消静音时改变图标
                tcbSound.Value = 100; //音量条变为100
                wmp.settings.volume = 100; //音量变为100
            }
        }
        private void picSound_MouseEnter(object sender, EventArgs e)
        {
            //鼠标进入喇叭时显示音量条
            tcbSound.Visible = true; 
        }
        private void picSound_MouseLeave(object sender, EventArgs e)
        {
            //鼠标离开喇叭但不进入音量条，音量条消失
            Point mousepoint = picSound.PointToClient(Control.MousePosition);//获取鼠标离开时的坐标
            if (mousepoint.Y >= 0 || mousepoint.X <= 0 || mousepoint.X >= 30) //离开的位置不在音量条内
            {
                tcbSound.Visible = false;
            }
        }
        private void tcbSound_MouseEnter(object sender, EventArgs e)
        {
            //鼠标进入音量条时显示音量条
            tcbSound.Visible = true; 
        }

        private void tcbSound_MouseLeave(object sender, EventArgs e)
        {
            //鼠标离开音量条时不显示音量条
            tcbSound.Visible = false;
        }
        bool allplay = true;//是否全部循环
        bool single = false;//是否单曲循环
        bool random = false;//是否随机播放
        private void picRetWeet_Click(object sender, EventArgs e)
        {
            if (allplay)
            {
                allplay = false;
                single = true;
                picRetWeet.BackgroundImage = Properties.Resources.single;//变成单曲循环图标
                lblPromptMsg.Text = "单曲循环";//提示信息变成单曲循环
            }
            else if (single)
            {
                single = false;
                random = true;
                picRetWeet.BackgroundImage = Properties.Resources.random1;//变成随机播放图标
                lblPromptMsg.Text = "随机播放";//提示信息变成随机播放
            }
            else if (random)
            {
                random = false;
                allplay = true;
                picRetWeet.BackgroundImage = Properties.Resources.retweet;//变成全部循环图标
                lblPromptMsg.Text = "全部循环";//提示信息变成全部循环
            }
        }

        private void picClose_MouseEnter(object sender, EventArgs e)
        {
            //鼠标进入时关闭按钮变红
            picClose.BackColor = Color.Red; 
        }

        private void picClose_MouseLeave(object sender, EventArgs e)
        {
            //鼠标离开时关闭按钮变透明
            picClose.BackColor = Color.Transparent; 
        }

        private void picClose_Click(object sender, EventArgs e)
        {
            //鼠标点击时关闭窗体
            this.Close();
        }

        bool isDrag;//是否拖动窗体
        Point clickPoint;
        private void FrmMain_MouseDown(object sender, MouseEventArgs e)
        {
            //鼠标点下开始拖动窗体
            isDrag = true;
            clickPoint = new Point(e.X, e.Y);
        }

        private void FrmMain_MouseMove(object sender, MouseEventArgs e)
        {
            //拖动窗体
            if (isDrag)
            {
                this.Location = new Point(this.Location.X + e.X - clickPoint.X, this.Location.Y + e.Y - clickPoint.Y);
            }
        }

        private void FrmMain_MouseUp(object sender, MouseEventArgs e)
        {
            //拖动结束
            isDrag = false;
        }

        private void picProgress_MouseClick(object sender, MouseEventArgs e)
        {
            //点击进度条改变歌曲位置
            progress = e.X;
            double per = (double)e.X / (double)picProgress.Width;
            wmp.Ctlcontrols.currentPosition = wmp.currentMedia.duration * per;
            ghpro.Clear(Color.Gray);
            currentLyricLine = 0;
        }

        private void picRetWeet_MouseEnter(object sender, EventArgs e)
        {
            //鼠标进去显示提示信息
            lblPromptMsg.Visible = true;
        }

        private void picRetWeet_MouseLeave(object sender, EventArgs e)
        {
            //鼠标离开关闭提示信息
            lblPromptMsg.Visible = false;
        }
    }
}
